<template>
  <div class="work">

    <div class="search">
      <div class="title">
        PE Salary Management
      </div>
      <div class="dateSearch">
        <div>
          <el-date-picker
            v-model="form.startTime"
            type="month"
            value-format='yyyy-MM'
            :picker-options="startTime"
            placeholder="start time">
          </el-date-picker>
          <el-date-picker
            v-model="form.endTime"
            type="month"
            value-format='yyyy-MM'
            :picker-options="endTime"
            placeholder="end time">
          </el-date-picker>
        </div>
        <div>
          <el-button type="info" plain @click="search">search</el-button>
          <el-button type="info" plain @click="clearSearch" style="margin-left:0px;">cancel search</el-button>
        </div>
      </div>
    </div>

    <!-- 主体内容 -->
    <template>
      <el-table
        :header-cell-style="{background:'#F2F6FC',color:'#606266'}"
        :data="tableData"
        border
        style="width: 100%"
        ref="tableData"
        class="emp_table"
        v-if="tableData">
        <el-table-column
          align="center"
          prop="salary_id"
          label="Payroll ID"
          width="180">
        </el-table-column>
        <el-table-column
          align="center"
          prop="belong_date"
          label="Belong Date">
        </el-table-column>
        <el-table-column
          align="center"
          prop="submit_time"
          label="Submit Time">
        </el-table-column>
        <el-table-column
          align="center"
          prop="pe_name"
          label="Prepared by">
        </el-table-column>
        <el-table-column
          align="center"
          prop="number_of_pe"
          label="Number of PE">
          <template slot-scope="{ row }">
            {{ row.number_of_pe }}
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="status_name"
          label="Status">
          <template slot-scope="{ row }">
            {{ row.status_name || '暂无' }}
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          label="Action">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="seeForm(scope.row.salary_id)">see details</el-button>
          </template>
        </el-table-column>
      </el-table>

    </template>
    <el-pagination
      align="right"
      @current-change="curChange"
      :current-page.sync="curPage"
      :page-size="10"
      layout="total,prev, pager, next, jumper"
      :total="count">
    </el-pagination>

    <seeForm ref="seeForm"></seeForm>

  </div>


</template>

<script>
import {delSalaryList,getSalaryList} from '@/request/other_api'
import seeForm from './model/seeForm'

export default {
  components:{
    seeForm,
  },
  data() {
    return {
      // 搜索内容
      searchName:'',
      // 页面数据
      tableData: [],
      // count:总数据量
      count:0,
      // 当前页
      curPage:1,
      //搜索时间
      form:{
        startTime:undefined,
        endTime:undefined,
      },
      // 是否在搜索状态
      isSearch:false,
      startTime: {
        disabledDate: time => {
          let endDateVal = this.form.endTime;
          if (endDateVal) {  return time.getTime() > new Date(endDateVal).getTime()}
        }
      },
      endTime: {
        disabledDate: time => {
          let beginDateVal = this.form.startTime;
          if (beginDateVal) { return time.getTime() < new Date(beginDateVal).getTime()}
        }
      },
      key:Number
    }
  },
  created() {
    getSalaryList().then(res =>{
      if(res.code == 200) {
        this.tableData = res.data
        this.count = res.count
      }
    })
  },
  methods: {
    //获取全部列表数据分页
    getSalaryList(){
      getSalaryList({
        page:this.curPage
      }).then(res =>{
        if(res.code == 200) {
          this.tableData = res.data
          this.count = res.count
        }
      })
    },
    //获取搜索列表数据分页
    getSalaryPageList(){
      getSalaryList({
        startDate:this.form.startTime,
        endDate:this.form.endTime,
        page:this.curPage
      }).then(res =>{
        if (res.code == 200) {
          this.tableData = res.data
          this.count = res.count
        }
      })
    },
    // 搜索
    search(){
      this.isSearch = true
      getSalaryList({
        startDate:this.form.startTime,
        endDate:this.form.endTime,
      }).then(res =>{
        if (res.code == 200) {
          this.tableData = res.data
          this.count = res.count
        }
      })
      this.curPage = 1
    },
    // 取消搜索
    clearSearch(){
      this.isSearch = false
      getSalaryList().then(res =>{
        if(res.code == 200) {
          this.tableData = res.data
          this.count = res.count
        }
      })
      this.form.startTime = ''
      this.form.endTime = ''
      this.curPage = 1
    },
    //  点击每页刷新获取数据
    curChange (currentPage) {
      this.curPage = currentPage
      this.getSalaryPageList()
    },
    // 上一页
    prevClick () {
      this.curPage < 1 ? this.curPage = 1 : this.curPage--
      this.getSalaryPageList()
    },
    // 下一页
    nextClick () {
      this.curPage >= this.count ? this.curPage = this.count : this.curPage++
      this.getSalaryPageList()
    },
    //跳转到查看工资单详情
    seeForm(salary_id){
      let routeData = this.$router.resolve(`/payAdminDetail/${salary_id}`)
      window.open(routeData.href, '_blank')
    },
  },
}
</script>

<style lang="less" scoped>
.search {
  display: flex;
  justify-content: space-between;
  padding-bottom: 20px;
}
.dateSearch {
  display: flex;
  justify-content: space-between;
  padding-bottom: 0px;
}
// /deep/ .el-input__inner{
//   border-radius: 0;
// }
/deep/ .el-button {
  border-radius: 0;
}
.title{
  font-family: SourceHanSansSC;
  font-weight: 700;
  font-size: 20px;
  color: rgb(16, 16, 16);
  font-style: normal;
  letter-spacing: 0px;
  line-height: 30px;
  text-decoration: none;
  margin-bottom: 10px;
}
</style>
